package com.mlnx.web.ui.controller;

import com.mlnx.common.entity.Response;
import com.mlnx.ui.pojo.dto.PatientAlarmDTO;
import com.mlnx.ui.pojo.dto.PatientDeviceDTO;
import com.mlnx.ui.service.AppService;
import com.mlnx.ui.service.PatientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.shan.spring.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;

@Api(value = "app接口", tags = {"app接口"})
@RestController
@RequestMapping("app")
public class AppController extends BaseController {
    @Autowired
    private PatientService patientService;

    @Autowired
    private AppService appService;


    @ApiOperation(value = "获取最新版本")
    @GetMapping(value = "/version")
    public Response getVersion() {
        return result(appService.getVersion());
    }

    @ApiOperation(value = "apk下载")
    @RequestMapping(value = "/update")
    public void getApk(HttpServletResponse response) {

        File d = new File("");
        File file = new File(d.getAbsolutePath() + File.separator + "app");

        File apk = file.listFiles()[0];

        response.setCharacterEncoding("utf-8");
        response.setContentType("multipart/form-data");
        response.setHeader("Content-Disposition",
                "attachment;fileName=" + apk.getName());
        response.setContentLength((int) (apk.length()));

        InputStream inputStream;
        try {
            inputStream = new FileInputStream(apk);

            OutputStream os = response.getOutputStream();
            byte[] b = new byte[(int) apk.length()];
            int length;
            while ((length = inputStream.read(b)) > 0) {
                os.write(b, 0, length);
            }

            os.close();

            inputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @ApiOperation(value = "通过群组ID获取患者")
    @GetMapping(value = "/patient")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupId", value = "群组ID", required = true)
    })
    public Response getPatientByGroupId(@RequestParam("groupId") Integer groupId) {
        List<PatientDeviceDTO> patientDeviceDTOList = patientService.getPatientsByGroupId(groupId);

        return result(patientDeviceDTOList);
    }


    @ApiOperation(value = "通过患者ID获取患者及当前绑定设备信息")
    @GetMapping(value = "/patient/device")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "patientId", value = "患者", required = true)
    })
    public Response getPatientDeviceInfo(@RequestParam("patientId") Integer patientId) {
        PatientAlarmDTO patientAlarmDTO = patientService.getPatientDeviceInfo(patientId);

        return result(patientAlarmDTO);
    }
}
